package frame2;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.datatransfer.Clipboard;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.LayoutFocusTraversalPolicy;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.text.DefaultCaret;

public class Convert extends JFrame implements ActionListener{
	
	private JTextPane inputArea = new JTextPane();
	private JScrollPane inputAreaScrollPane = new JScrollPane(this.inputArea);
	
	private JButton runButton = new JButton("Convert");
	
	private JTextPane outputArea = new JTextPane();
	private JScrollPane outputAreaScrollPane = new JScrollPane(this.outputArea);
	
	
	private JPopupMenu popup;
	private Clipboard clipboard;
	
	
	private class BasicWindowMonitor extends WindowAdapter {
		@Override
		public void windowClosing(WindowEvent e) {
			handleClose();
		}
	}
	
	private void handleClose() {
		setVisible(false);
		dispose();
		System.exit(0);
	}
	
	public Convert()
	{
		super("Encription");
		
		Dimension minimumSize = new Dimension(800, 450);
		this.setMinimumSize(minimumSize);
		
		this.addWindowListener(new BasicWindowMonitor());
		
		this.runButton.addActionListener(this);
		
		// With LayoutFocusTraversalPolicy Java determines the next focus
		// component based on layout
		this.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy());
		
		// Enable the ENTER key to act as trigger for focused button
		UIManager.put("Button.defaultButtonFollowsFocus", Boolean.TRUE);
		
		initializeGraphics();
		
	}
	
	private void initializeGraphics() {
		setLayout(new GridBagLayout());
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		gbc.insets.bottom = 5;
		gbc.insets.top = 5;
		gbc.insets.left = 5;
		gbc.insets.right = 5;
		
		add(new JLabel("Input Text"), gbc);
		
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbc.weightx = 10;
		gbc.weighty = 10;
		gbc.fill = GridBagConstraints.BOTH;
		
		
		this.inputArea.setEditable(true);
		// Use monospaced font so tables look nice
		Font font = Font.decode(Font.MONOSPACED);
		this.inputArea.setFont(font);
		this.inputAreaScrollPane.setSize(400,400);
		// Set the caret to update position automatically as text is appended
		DefaultCaret caret = (DefaultCaret) this.inputArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		// Always display scroll bars
		this.inputAreaScrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		this.inputAreaScrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		//this.inputAreaScrollPane.setBorder(BorderFactory.createEtchedBorder());
		this.add(this.inputAreaScrollPane, gbc);
		
		
		
		// Line separator
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;
		gbc.gridy = 11;
		gbc.gridheight = 1;
		gbc.gridwidth = 16;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
		
		
		
		gbc.gridy = 12;
		
		this.runButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		this.runButton.setToolTipText("Process all files and directories in the file list panel.");
		
		//change the button color when mouse over the button
		runButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                runButton.setBackground(Color.GREEN);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                runButton.setBackground(UIManager.getColor("control"));
            }
        });
		add(this.runButton, gbc);
		
		
		
		// Line separator
		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;
		gbc.gridy = 13;
		gbc.gridwidth = 16;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		add(new JSeparator(SwingConstants.HORIZONTAL), gbc);
		
		

		gbc.weightx = 1;
		gbc.weighty = 1;
		gbc.gridx = 0;
		gbc.gridy = 14;
		gbc.fill = GridBagConstraints.CENTER;
		add(new JLabel("Output Text"), gbc);
		
		

		gbc.gridx = 0;
		gbc.gridy = 15;
		gbc.weightx = 10;
		gbc.weighty = 10;
		gbc.fill = GridBagConstraints.BOTH;
		
		this.outputArea.setEditable(false);
		// Use monospaced font so tables look nice
		//Font font = Font.decode(Font.MONOSPACED);
		this.outputArea.setFont(font);
		this.outputAreaScrollPane.setSize(400,400);
		// Set the caret to update position automatically as text is appended
		//DefaultCaret caret = (DefaultCaret) this.outputArea.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		// Always display scroll bars
		this.outputAreaScrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		this.outputAreaScrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		//this.outputAreaScrollPane.setBorder(BorderFactory.createEtchedBorder());
		this.add(this.outputAreaScrollPane, gbc);
			
		
	}


	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == this.runButton) {
			this.outputArea.setText(this.inputArea.getText());
			
		}	
	}
	
	public static void main(String args[])
	{
		Convert c = new Convert();
		c.setVisible(true);
	}

}
